package com.tanhua.dubbo.api;

import com.tanhua.manage.domain.mongo.Friend;
import com.tanhua.manage.domain.mongo.RecommendUser;
import com.tanhua.manage.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 交友
     */
    @Override
    public void makeFriend(Long loginUserId, Long targetUserId) {

        //构建friend对象
        Friend friend = new Friend();
        friend.setUserId(loginUserId);
        friend.setFriendId(targetUserId);
        long timeMillis = System.currentTimeMillis();
        friend.setCreated(timeMillis);
        //判断是否已经加过好友,如果不是再添加
        Query query = new Query(Criteria.where("userId").is(loginUserId).and("friendId").is(targetUserId));
        if (!mongoTemplate.exists(query,Friend.class)){
            //添加好友，将friend类添加到库中
            mongoTemplate.insert(friend);
        }
        //判断对方是否加我好友
        query = new Query(Criteria.where("userId").is(targetUserId).and("friendId").is(loginUserId));
        if (!mongoTemplate.exists(query,Friend.class)){
            friend.setUserId(targetUserId);
            friend.setFriendId(loginUserId);

            mongoTemplate.insert(friend);
        }

    }

    /**
     * 联系人分页查询
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPage(Long userId, Long page, Long pageSize) {

        Query query = new Query(Criteria.where("userId").is(userId));
        Long total = mongoTemplate.count(query,Friend.class);
        List<Friend> friends = new ArrayList<>();
        if (total>0){
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            friends = mongoTemplate.find(query, Friend.class);
        }
        return PageResult.pageResult(page,pageSize,friends,total);
    }

    /**
     * 通过用户id统计好友数量
     *
     * @param
     * @return
     */
    @Override
    public Long countByUserId(Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query,Friend.class);
    }

    /**
     * 好友分页查询 加上缘分
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageWithScore(Long userId, Long page, Long pageSize) {

        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);
        List<RecommendUser> userList = new ArrayList<>();
        if (count>0){
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            List<Friend> friends = mongoTemplate.find(query, Friend.class);
            List<Long> friendIds = friends.stream().map(Friend::getFriendId).collect(Collectors.toList());

            Query recommendUserQuery = new Query(Criteria.where("toUserId").is(userId).and("userId").in(friendIds));

            userList= mongoTemplate.find(recommendUserQuery,RecommendUser.class);
        }
        return PageResult.pageResult(page,pageSize,userList,count);
    }
}
